package com.blackline.auto.Report;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;

public class Schedule_UPS_report {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	int i=0;
	String NameofReport="01";
	@Before
	public void setUp() throws Exception {
		
	}
	

	@After
	public void tearDown() throws Exception {
		
		
		
		
	}

	@Test
	public void Schedule_UPS_test() throws Exception {
		
		//driver.get(pa.urlDev2);
		//to get the current date and set its as start date  also append it in name .
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar cal=Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String startDate=sdf.format(cal.getTime());
		logger.info(startDate);

		// current time  to append it in name
		SimpleDateFormat sdf1=new SimpleDateFormat("hh:mm");
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(now);
		cal1.add(Calendar.DAY_OF_MONTH, 1);
		String startTime=sdf1.format(cal1.getTime());
		logger.info(startTime);
		
		
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy");
		String years=sdf2.format(cal.getTime());
		String endDate="12/31/"+years;
		logger.info(endDate);
		
		
		logger.info("You've selected site: https://t2.blacklineondemand.com/Test3");
		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test2", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		
		  UI.NavigationMenu.REPORTS.click();
		  UI.Reports.Enhanced_Reporting_link.click();
		  Thread.sleep(20000);
		  
		  //ups Reports 
		  
		  UI.Reporting.AddIcon_UPSReports.click();
		  UI.Reporting.UPS_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  
		  UI.Reporting.Name_text_field.clear();
	 		UI.Reporting.Name_text_field.sendKeys(NameofReport);
	 		UI.Reporting.Refresh_button.click();
	 		
	  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
	 logger.info(schedulename);
	  
	 
	 
	 assertNotNull(UI.Reporting.There_are_no_records_text);
	 // enter the scheduling 
	 UI.Reporting.Schedule_linkOfSearchedResult.click();
	 
	 
	 UI.SchedulePrompts.Schedule_Name_Text.clear();
	 UI.SchedulePrompts.Schedule_Name_Text.sendKeys("UPS"+NameofReport+startDate+startTime);
	 // edit of start date
	 UI.SchedulePrompts.Schedule_Start_Date_Text.clear();
	 UI.SchedulePrompts.Schedule_Start_Date_Text.sendKeys(startDate);
	 //editing end date 
	 UI.SchedulePrompts.Schedule_End_Date_Text.clear();
	 UI.SchedulePrompts.Schedule_End_Date_Text.sendKeys(endDate);
	 
	 UI.SchedulePrompts.Schedule_Report_button.click();
	 
	 Thread.sleep(20000);
	// Thread.sleep(25000);
	 // missed the box and click on ohk .....

	 String runRes=UI.Alert_inReporting.Message_Text.getText();
	 
	
		if(runRes.contains("has been created"))
		  {
			  logger.info("[Pass!] "+runRes);
			  UI.Alert_inReporting.OK_button.click();
		  }
		
		  else{
			  logger.error("[Fail!] "+runRes);
			  verificationErrors.append("[Fail!] "+runRes);
			  fail("Schedule failed: "+runRes);
		  }
	 
		
		
		
		NameofReport="UPS"+NameofReport+startDate+startTime;
		
		
		verify_schedule(NameofReport);
		  
		
		
	}
	/*
	 * 
	 * verifying the user is able to schedule
	 */
	public void verify_schedule(String report_name) throws Exception
	{

		UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  // reaching till scheduled report 
		  UI.Reporting.Name_text_field.clear();
		  UI.Reporting.Name_text_field.sendKeys(report_name);
		  
		  logger.info(report_name);
		  
		  
		  UI.Reporting.Refresh_button.click();
		  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
		 logger.info(schedulename);
		  assertNotNull(UI.Reporting.There_are_no_records_text	);
		  Thread.sleep(2000);
		  
		 // String result= UI.Reporting.Number_of_results.getText();
		  
		  String Results= UI.Reporting.Number_of_results.getText(); 
			String delims = " ";
			String[] tokens = Results.split(delims);
			int number_of_results=Integer.parseInt(tokens[3]);
		  
		  // number of reports for UPS is constant -"24"
		  
		  if(number_of_results==1)
		  {
			  logger.info("[Pass!] "+"schedule  verified ");
			  UI.Alert_inReporting.OK_button.click();
		  }
		
		  else{
			  logger.error("[Fail!] "+"schedule  verified ");
			  verificationErrors.append("[Fail!] "+"schedule  verified ");
			  fail("Schedule failed: "+Results);
		  }
	 

	  Thread.sleep(2000);
		
	  
	  driver.quit();

		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	
	
}
